﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class ItemManager : Singleton<ItemManager> {

    public List<Item> _itemList;

    public ItemManager()
    {
        _itemList = LoadItem();
        if (_itemList == null)
            _itemList = new List<Item>();
    }

    private List<Item> LoadItem()
    {
        return Utils.LoadByJson<List<Item>>(Application.dataPath, "itemList.json");
    }

	public void ReplaceItem(int index, Item item){
		if (index < _itemList.Count && index >= 0) {
			_itemList[index] = item;
		}else {
			_itemList.Add(item);
		}
	}

	public void RemoveItem(int index){
		if (index < _itemList.Count && index >= 0){
			_itemList.RemoveAt(index);
		}
        SaveList();
	}

	public void InsertItem(int index){
        if (index < _itemList.Count && index >= 0)
        {
            _itemList.Insert(index,new Item());
        }else 
            _itemList.Add(new Item());
	}

    public int GetRepeatId(){
        for (int i = 0; i < _itemList.Count - 1; i++)
        {
            for (int y = i + 1; y < _itemList.Count; y++)
            {
                int preId = _itemList[i].id;
                int nexId = _itemList[y].id;
                if (preId == nexId) return preId;
            }
        }
        return -1;
    }

	public void SaveList(){
        Utils.SaveByJson<List<Item>>(Application.dataPath, "itemList.json", _itemList);
	}
}
